package andestours.interfaz;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import andestours.mundo.AndesTours;
import andestours.mundo.DestinoValue;
import andestours.mundo.ServicioValue;
import andestours.mundo.TransporteValue;


public class ServletTransporte extends ServletTemplate {

	private static final long serialVersionUID = 1L;
	
	private DestinoValue destino;
	private ServicioValue servicio;
	private TransporteValue transporte;

	@Override
	public void imprimirContenido(HttpServletRequest request, PrintWriter out) {
		
		if(request.getParameter("destino") == null || request.getParameter("servicio") == null) {
			out.println("<br /><br /><br />Ha ocurrido un error. Por favor vuelva al inicio e intente de nuevo.");
			return;
		}
		
		destino = AndesTours.getInstance().darDestino(Integer.parseInt(request.getParameter("destino")));
		servicio = AndesTours.getInstance().darServicio(request.getParameter("servicio"));
		
		boolean consulto = request.getParameter("costo") != null;
		boolean selecciono = request.getParameter("transporte") != null;
		
		ArrayList<TransporteValue> mediosTransporte = new ArrayList<TransporteValue>();
		
		if(consulto || selecciono) {
			Date inicio = new Date();
			mediosTransporte = AndesTours.getInstance().darMediosTransporte(destino.getCodigo(), Integer.parseInt(request.getParameter("costo")));
			Date fin = new Date();
			System.out.println("RF3 Dar medios de transporte: " + (fin.getTime() - inicio.getTime()) + " milisegundos.");
		}
		
		if(selecciono) {
			Date inicio = new Date();
			transporte = AndesTours.getInstance().darTransporte(Integer.parseInt(request.getParameter("transporte")));
			Date fin = new Date();
			System.out.println("RF3 Mostrar transporte: " + (fin.getTime() - inicio.getTime()) + " milisegundos.");
		}
		
		out.println(" <tr> ");
		out.println(" 	<td width='35%' bgcolor='#6666aa' valign='baseline'> ");
		out.println(" 		<center> ");
		out.println(" 			<font face='Verdana, Arial, Helvetica, sans-serif' color='#FF9966'><b>3. Medio de transporte</b></font> ");
		out.println(" 			<br /><br /><br /><br /><br /> ");
		out.println(" 			<form action='transporte.html' method='post' onsubmit='return validarTransporte()'> ");
		out.println(" 				<font face='Verdana, Arial, Helvetica, sans-serif'>Costo m&aacute;ximo<br /> ");
		out.println(" 				$ </font><input type='text' name='costo' maxlength='9' size='8' value='" + (consulto || selecciono ? request.getParameter("costo") : "") + "' /> ");
		out.println(" 				<br /><br /><br /> ");
		out.println(" 				<input type='hidden' name='destino' value='" + destino.getCodigo() + "' /> ");
		out.println(" 				<input type='hidden' name='servicio' value='" + servicio.getCodigo() + "' /> ");
		out.println("				<input type='hidden' id='ida' name='ida' value='" + request.getParameter("ida") + "' />");
		out.println("				<input type='hidden' id='vuelta' name='vuelta' value='" + request.getParameter("vuelta") + "' />");
		out.println(" 				<input type='submit' value='Buscar' /> ");
		out.println(" 			</form> ");
		out.println(" 				<br /><br /><br /><br /> ");
		out.println(" 				<font face='Verdana, Arial, Helvetica, sans-serif'>Medios disponibles</font> ");
		out.println(" 				<br /> ");
		out.println(" 			<form action='transporte.html' method='post' > ");
		out.println(" 				<select name='transporte' id='transporte' " + (consulto || selecciono ? "" : "disabled='disabled'") + " > ");
		out.println(" 					<option value='0' onclick=\"javascript:document.forms[2].boton.disabled='disabled'\">--- Seleccione ---</option> ");
		
		for(int i=0; i<mediosTransporte.size(); i++) {
			boolean sel = selecciono ? mediosTransporte.get(i).getCodigo() == transporte.getCodigo() : false;
			out.println(" 			<option value='" + mediosTransporte.get(i).getCodigo() + "' onclick='javascript:escogerServicio()' " + (sel ? "selected='selected'" : "") + ">" + mediosTransporte.get(i).getEmpresa() + "</option> ");
		}
			
		out.println(" 				</select> ");
		out.println(" 				<input type='hidden' name='destino' value='" + destino.getCodigo() + "' /> ");
		out.println(" 				<input type='hidden' name='servicio' value='" + servicio.getCodigo() + "' /> ");
		out.println("				<input type='hidden' name='costo' value='" + request.getParameter("costo") + "' /> ");	
		out.println("				<input type='hidden' id='ida' name='ida' value='" + request.getParameter("ida") + "' />");
		out.println("				<input type='hidden' id='vuelta' name='vuelta' value='" + request.getParameter("vuelta") + "' />");
		out.println(" 				</form> ");
		out.println(" 			<br /><br /><br /> ");
		out.println(" 			<form action='reserva.html' method='post'> ");
		out.println(" 				<input type='hidden' name='destino' value='" + destino.getCodigo() + "' /> ");
		out.println(" 				<input type='hidden' name='servicio' value='" + servicio.getCodigo() + "' /> ");
		out.println("				<input type='hidden' name='transporte' value='" + (selecciono ? transporte.getCodigo() : 0) + "' /> ");
		out.println("				<input type='hidden' id='ida' name='ida' value='" + request.getParameter("ida") + "' />");
		out.println("				<input type='hidden' id='vuelta' name='vuelta' value='" + request.getParameter("vuelta") + "' />");
		out.println(" 				<input type='submit' name='boton' id='boton' value='Continuar' " + (selecciono ? "" : "disabled='disabled'") + " /> ");
		out.println(" 			</form> ");
		out.println(" 		</center> ");
		out.println(" 	</td> ");
		out.println(" 	<td valign='baseline'><center> ");
		out.println("                <font face='Verdana, Arial, Helvetica, sans-serif' color='#6666aa' size='+2'>" + destino.getNombre() + "</font> <br /> ");
		out.println("                <font face='Times New Roman, Times, serif'>" + destino.getPais() + " - " + destino.getContinente() + "</font> <br /> ");
		out.println("                <img border='2' height='150' src='" + destino.getFoto() + "' /> <br /> ");
		out.println("                <br /><br /> ");
		out.println("                <font face='Verdana, Arial, Helvetica, sans-serif' color='#6666aa' size='+2'>" + servicio.getNombre() + "</font> <br />");
		for(int i=0; i<servicio.getTipo(); i++)
			out.println("&#10029;");
		out.println("				 <br />Restaurante " + servicio.getRestaurante());
		out.println("<br /><br /><br /><br /><br />");
		
		if(selecciono) {
			out.println(" 			<font face='Verdana, Arial, Helvetica, sans-serif' color='#6666aa' size='+2'>" + transporte.getEmpresa() + "</font> ");
			out.println(" 			<br />Tipo: " + transporte.getTipo());
			out.println("			<br />Puestos: " + transporte.getPuestos());
			out.println("			<br />" + (transporte.getCupos() <= 0 ? "<font color='#ff0000'>" : "<font>") + "Disponibles: " + transporte.getCupos() + "</font>");
			out.println(" 			<br /><br /> ");
			out.println(" 			<div align='justify'> ");
			out.println(" 			<font face='Times New Roman, Times, serif'>N&uacute;mero del viaje: " + transporte.getNum_Viaje() + "<br />Ciudad de origen: " + transporte.getCiudad_Origen() + "<br />Ciudad de destino: " + transporte.getCiudad_Destino() + "<br />Costo: $" + transporte.getCosto() + "<br /><br /> ");
			out.println(" 			</font></div> ");
		}
		
		out.println("		</center>");
		out.println("    </td> ");
		out.println(" </tr> ");
	}
}
